package com.htfyun.ft.fragment.engineer;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.TextView;

import com.htfyun.ft.R;
import com.htfyun.ft.agingtest.AgingTestItemAttr;
import com.htfyun.ft.fragment.BaseFragment;
import com.htfyun.ft.fragment.factory.FactoryTestItemsEnum;
import com.htfyun.ft.fragment.factory.ResultAttr;
import com.htfyun.ft.utils.DebugLog;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

/**
 * Created by sst on 2018/7/11.
 */

public class FragmentFTForceFailed extends BaseFragment
        implements View.OnClickListener {

    private final static String TAG = FragmentFTForceFailed.class.getSimpleName();


    private GridView gridView;
    private MyAdapter myAdapter;
    @Override
    protected int getLayoutRes() {
        return R.layout.fragment_engineer_ft_force_failed;
    }

    @Override
    protected void onInitView(@Nullable Bundle savedInstanceState) {

        gridView = (GridView) findViewById(R.id.gridView);
        List<FactoryTestItemsEnum> list = new ArrayList<>();
        for (FactoryTestItemsEnum itemsEnum : FactoryTestItemsEnum.values()) {
            if (itemsEnum.getNVRomKey() != null) {
                list.add(itemsEnum);
            }
        }
        myAdapter = new MyAdapter(getContext(), -1, list);
        gridView.setAdapter(myAdapter);

        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                FactoryTestItemsEnum itemsEnum = myAdapter.getItem(position);
                if (itemsEnum != null) {
                    itemsEnum.setResult(ResultAttr.FAIL);
                    myAdapter.notifyDataSetChanged();
                }
            }
        });

        initBtn(R.id.btnAllFail);
        initBtn(R.id.btnClearAging);

    }

    private void initBtn(int resId) {
        View btn = findViewById(resId);
        if (btn != null) {
            btn.setOnClickListener(this);
        }
    }

    @Override
    protected void onVisible() {
        super.onVisible();
    }

    @Override
    public void onClick(View v) {
        final int id = v.getId();
        switch (v.getId()) {
            case R.id.btnClearAging:
                AgingTestItemAttr.clearAgingAll();

                break;
            case R.id.btnAllFail:
                for (FactoryTestItemsEnum item : FactoryTestItemsEnum.values()) {
                    if (item.getItemFragment() != null) {
                        item.setResult(ResultAttr.FAIL);
                    }
                }
                myAdapter.notifyDataSetChanged();
                break;
        }

    }

    private class MyAdapter extends ArrayAdapter<FactoryTestItemsEnum> {


        public MyAdapter(@NonNull Context context, int resource, @NonNull List<FactoryTestItemsEnum> objects) {
            super(context, resource, objects);
        }

        @NonNull
        @Override
        public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
            ViewHolder holder = null;
            if (convertView != null) {
                holder = (ViewHolder)convertView.getTag();
            } else {
                holder = new ViewHolder();
                LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                convertView = inflater.inflate(R.layout.recycler_item_factory_main_test_items, null);
                convertView.setClickable(false);
                convertView.setFocusable(false);
                holder.title = (TextView) convertView.findViewById(R.id.title);
                holder.summary = (TextView) convertView.findViewById(R.id.summary);

                convertView.setTag(holder);
            }

            FactoryTestItemsEnum itemsEnum = getItem(position);
            if (itemsEnum != null) {

                holder.title.setText(itemsEnum.getName());
                holder.summary.setText(itemsEnum.getResult().getResult());
                convertView.setBackground(itemsEnum.getDrawable());

            }

            return convertView;
        }
    }

    private final class ViewHolder {
        TextView title;
        TextView summary;
    }

}
